package com.yz.jdd.module.main.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.yz.jdd.R;
import com.yz.jdd.network.response.Response_OrderList;

import java.util.List;

/**
 *  普通订单列表 (POS点餐 , 外卖订单 , 异常订单)
 */
public class OrdersAdapter extends BaseExpandableListAdapter {

    private Context context;
    private List<Response_OrderList.ResultObjectBean.PageDataBean> responseOrderLists;

    public OrdersAdapter(Context context, List<Response_OrderList.ResultObjectBean.PageDataBean> orderListItemList) {
        this.context = context;
        this.responseOrderLists = orderListItemList;
    }

    @Override
    public int getGroupCount() {
        return responseOrderLists.size();
    }

    @Override
    public int getChildrenCount(int i) {
        return responseOrderLists.get(i).getOrderList().size();
    }

    @Override
    public Object getGroup(int i) {
        return responseOrderLists.get(i);
    }

    @Override
    public Object getChild(int i, int i1) {
        return responseOrderLists.get(i).getOrderList().get(i1);
    }

    @Override
    public long getGroupId(int i) {
        return i;
    }

    @Override
    public long getChildId(int i, int i1) {
        return i1;
    }

    //分组和子选项是否持有稳定的ID, 就是说底层数据的改变会不会影响到它们
    @Override
    public boolean hasStableIds() {
        return true;
    }

    //指定位置上的子元素是否可选中
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    @Override
    public View getGroupView(int i, boolean b, View convertView, ViewGroup parent) {
        GroupViewHolder groupViewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_order_list_partent, parent, false);
            groupViewHolder = new GroupViewHolder();
            groupViewHolder.date_tv = (TextView) convertView.findViewById(R.id.date_tv);
            groupViewHolder.count_tv = (TextView) convertView.findViewById(R.id.count_tv);
            convertView.setTag(groupViewHolder);
        } else {
            groupViewHolder = (GroupViewHolder) convertView.getTag();
        }
        groupViewHolder.date_tv.setText(responseOrderLists.get(i).getDate());
        groupViewHolder.count_tv.setText("统计笔数:" + responseOrderLists.get(i).getCount());

        return convertView;

    }

    @Override
    public View getChildView(int i, int i1, boolean b, View convertView, ViewGroup parent) {
        ChildViewHolder childViewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_normal_order_list, parent, false);
            childViewHolder = new ChildViewHolder();
            childViewHolder.paytype_image = (ImageView) convertView.findViewById(R.id.paytype_image);
            childViewHolder.shop_name_tv = (TextView) convertView.findViewById(R.id.shop_name_tv);
            childViewHolder.time_tv = (TextView) convertView.findViewById(R.id.time_tv);
            childViewHolder.money_tv = (TextView) convertView.findViewById(R.id.money_tv);
            childViewHolder.state_tv = (TextView) convertView.findViewById(R.id.state_tv);
            childViewHolder.line = (View) convertView.findViewById(R.id.line);

            convertView.setTag(childViewHolder);

        } else {
            childViewHolder = (ChildViewHolder) convertView.getTag();
        }
        childViewHolder.shop_name_tv.setText(responseOrderLists.get(i).getOrderList().get(i1).getShopName());
        childViewHolder.time_tv.setText(responseOrderLists.get(i).getOrderList().get(i1).getTradeDate());
        childViewHolder.money_tv.setText( responseOrderLists.get(i).getOrderList().get(i1).getReceiveMoney()); // "¥ " +

        String orderStatus = "";
        switch (responseOrderLists.get(i).getOrderList().get(i1).getOrderStatus()+"") {
            case "1":
                orderStatus = "已收款";
                break;
            case "2":
                orderStatus = "已退款";
                break;
            case "3":
                orderStatus = "退款中";
                break;
            case "4":
                orderStatus = "撤销中";
                break;
            case "5":
                orderStatus = "已撤销";
                break;
            case "6":
                orderStatus = "外卖已完成";
                break;
            case "7":
                orderStatus = "外卖已退款";
                break;
            case "8":
                orderStatus = "预约点餐已取餐";
                break;
            case "9":
                orderStatus = "预约点餐已退款";
                break;
            case "10":
                orderStatus = "预约点餐待制作";
                break;
            case "11":
                orderStatus = "预约点餐待取餐";
                break;
            case "12":
                orderStatus = "收款失败";
                break;
            case "13":
                orderStatus = "收款异常";
                break;
            case "14":
                orderStatus = "已关单";
                break;
            default:
                orderStatus = "未知";
                break;

        }
        childViewHolder.state_tv.setText(orderStatus);
        if (responseOrderLists.get(i).getOrderList().size()-1 == i1){
            childViewHolder.line.setVisibility(View.GONE);
        }else{
            childViewHolder.line.setVisibility(View.VISIBLE);
        }

//        支付方式payType
        switch (responseOrderLists.get(i).getOrderList().get(i1).getPayType()+"") {
            case "2": // type="2": 支付宝
                childViewHolder.paytype_image.setBackgroundResource(R.mipmap.zfb);
                break;
            case "5"://   type="5":微信
                childViewHolder.paytype_image.setBackgroundResource(R.mipmap.wx);
                break;
            case "3"://   type="3":会员卡
                childViewHolder.paytype_image.setBackgroundResource(R.mipmap.pay_vip);
                break;
            case "4"://    type="4":银联
                childViewHolder.paytype_image.setBackgroundResource(R.mipmap.ylsk);
                break;
            case "1"://    type="1":现金
                childViewHolder.paytype_image.setBackgroundResource(R.mipmap.pay_xianjin);
                break;
            case "0": //   type="0":其他
                childViewHolder.paytype_image.setBackgroundResource(R.mipmap.wx);
            case "7":
                childViewHolder.paytype_image.setBackgroundResource(R.mipmap.ysf);
                break;
            case "8": //余额支付
                childViewHolder.paytype_image.setBackgroundResource(R.mipmap.yezf);
                break;

        }
        return convertView;

    }

    static class GroupViewHolder {
        TextView date_tv;
        TextView count_tv;
    }

    static class ChildViewHolder {
        ImageView paytype_image;
        TextView shop_name_tv;
        TextView time_tv;
        TextView money_tv;
        TextView state_tv;
        View line;

    }
}
